import wx from 'weixin-js-sdk'
import {getSigntrue} from '@/api/common'

export function getAdaptiveSize(value) {
  let v = document.body.clientWidth / 350.0 * value
  return v
}

export function initWeixinShare(title, desc) {
  getSigntrue({url: location.href.split('#')[0]}).then(response => {
    let data = response.data
    wx.config({
      appId: data.appid,
      timestamp: data.timestamp,
      nonceStr: data.noncestr,
      signature: data.sign,
      jsApiList: ['updateAppMessageShareData']
    })
  }, error => {
  })
  wx.ready(function () {
    wx.updateAppMessageShareData({
      title: title, // 分享标题
      desc: desc,
      link: location.href.split('#')[0], // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
      imgUrl: document.location.protocol + '//' + window.location.host + require('@/assets/icon_logo.jpg'), // 分享图标
      success: function () {
        // 设置成功
      }
    })
  })
  wx.error(function (res) {
  })
}

export function getUserAgent() {
  if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
    //ios
    return 'ios'
  } else if (/(Android)/i.test(navigator.userAgent)) {
    //android
    return 'android'
  } else {
    //pc
    return 'pc'
  }
}

export function getSystemVersion() {
  let u = navigator.userAgent
  let userAgent = getUserAgent()
  let version = ''
  if (userAgent == 'ios') {
    // ios
    let regStr_saf = /OS [\d._]*/gi
    let verinfo = u.match(regStr_saf)
    version = 'IOS' + (verinfo + '').replace(/[^0-9|_.]/ig, '').replace(/_/ig, '.')
  } else if (userAgent == 'android') {
    // android
    version = 'Android' + u.substr(u.indexOf('Android') + 8, u.indexOf(';', u.indexOf('Android')) - u.indexOf('Android') - 8)
  } else if (u.indexOf('BB10') > -1) {
    // 黑莓bb10系统
    version = 'blackberry' + u.substr(u.indexOf('BB10') + 5, u.indexOf(';', u.indexOf('BB10')) - u.indexOf('BB10') - 5)
  } else if (u.indexOf('IEMobile') > -1) {
    // windows phone
    version = 'winphone' + u.substr(u.indexOf('IEMobile') + 9, u.indexOf(';', u.indexOf('IEMobile')) - u.indexOf('IEMobile') - 9)
  } else {
    userAgent = navigator.userAgent.toLowerCase()
    if (userAgent.indexOf('windows nt 5.0') > -1) {
      version = 'Windows 2000'
    } else if (userAgent.indexOf('windows nt 5.1') > -1 || userAgent.indexOf('windows nt 5.2') > -1) {
      version = 'Windows XP'
    } else if (userAgent.indexOf('windows nt 6.0') > -1) {
      version = 'Windows Vista'
    } else if (userAgent.indexOf('windows nt 6.1') > -1 || userAgent.indexOf('windows 7') > -1) {
      version = 'Windows 7'
    } else if (userAgent.indexOf('windows nt 6.2') > -1 || userAgent.indexOf('windows 8') > -1) {
      version = 'Windows 8'
    } else if (userAgent.indexOf('windows nt 6.3') > -1) {
      version = 'Windows 8.1'
    } else if (userAgent.indexOf('windows nt 6.2') > -1 || userAgent.indexOf('windows nt 10.0') > -1) {
      version = 'Windows 10'
    } else {
      version = 'Unknown'
    }
  }
  return version
}

//获取状态栏高度
export function getStatusBarHeight() {
  // let userAgent = getUserAgent()
  // if (userAgent == 'android' && window.android) {
  //   return window.android.getStatusBarHeight()
  // } else if (userAgent == 'ios') {
  //   return window.prompt('getStatusBarHeight')
  // } else {
  //   return 0;
  // }
}

//获取屏幕宽度
export function getWindowWidth() {
  // let userAgent = getUserAgent()
  // if (userAgent == 'android' && window.android) {
  //   return window.android.getWindowWidth()
  // } else if (userAgent == 'ios') {
  //   return window.prompt('getWindowWidth')
  // }
}

//获取app token
export function getAppToken() {
  let userAgent = getUserAgent()
  if (userAgent == 'android' && window.android) {
    return window.android.getAppToken()
  } else if (userAgent == 'ios') {
    return window.prompt('getAppToken')
  }
}

//获取app版本
export function getAppVersion() {
  let userAgent = getUserAgent()
  if (userAgent == 'android' && window.android) {
    return window.android.getVersionCode()
  } else if (userAgent == 'ios') {
    //return 15;
    return window.prompt('getVersionCode')
  }
}

export function openWindow(url) {
  let userAgent = getUserAgent()
  if (userAgent == 'android' && window.android) {
    window.android.openWindow(url)
  } else if (userAgent == 'ios') {
    window.webkit.messageHandlers.openWindow.postMessage(url)
  } else {
    this.$router.push(url)
  }
}

//打电话
export function makingCalls(phone) {
  let userAgent = getUserAgent()
  if (userAgent == 'android' && window.android) {
    window.android.makingCalls(phone)
  } else if (userAgent == 'ios') {
    window.webkit.messageHandlers.makingCalls.postMessage(phone)
  }
}

//关闭app 页面
export function finish() {
  let userAgent = getUserAgent()
  if (userAgent == 'android' && window.android) {
    window.android.finish()
  } else if (userAgent == 'ios') {
    window.webkit.messageHandlers.finish.postMessage('')
  }
}


//退出登录页
export function loginOut() {
  let userAgent = getUserAgent()
  if (userAgent == 'android' && window.android) {
    window.android.loginOut()
  } else if (userAgent == 'ios') {
    window.webkit.messageHandlers.loginOut.postMessage('')
  }
}


export function openNewUrl(url) {
  try {
    if (url.endsWith('http')) {
      openWindow(url)
    } else {
      openWindow(('https:' == document.location.protocol ? 'https' : 'http') + '://' + window.location.host + url)
    }
  } catch (err) {
    this.$router.push(url)
  }
}
